package com.gykfbxxh.imovie.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.app.utils.util.LogUtils;
import com.app.utils.util.ViewSizeUtils;
import com.gykfbxxh.imovie.R;

import java.util.HashMap;

/**
 * 公用的Dialog显示类
 * Created by xuxinghai on 15/11/5.
 */
public class DialogUtils implements View.OnClickListener {

    private static DialogUtils dialogUtils;

    private Dialog loadDialog;//等待dialog
    private TextView text_show;//等待提示文字
    private View progressView;//等待根布局
    private View defaultView;
    private TextView tvTitle;
    private TextView tvDesc;
    private TextView btnNega;
    private TextView btnPosi;
    private View btnDriver;
    private OnDialogBtnClickListener mOnDialogBtnClickListener;


    private DialogUtils() {
    }

    public static DialogUtils getInstance() {
        if (dialogUtils == null) {
            dialogUtils = new DialogUtils();
        }
        return dialogUtils;
    }

    /**
     * 清空所有的dialog
     */
    public void clearAllDialog(){
        dialog = null;
        loadDialog = null;
        progressView = null;
        defaultView = null;
    }

/******************************************* 等待弹窗 start **********************************************/

    /**
     * 获取进度弹框实例
     */
    public void showProgressDialog(Activity activity) {
        if (activity == null || activity.isFinishing()) {
            return;
        }
        try {
            getProgress(activity);
            if (!loadDialog.isShowing()) {
                loadDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取进度弹框实例
     */
    public void showProgressDialog(Activity activity, String tip) {
        if (activity == null || activity.isFinishing()) {
            return;
        }
        try {

            getProgress(activity);
            if (!TextUtils.isEmpty(tip)) {
                text_show.setText(tip);
            }
            if (!loadDialog.isShowing()) {
                loadDialog.show();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 取消进度弹框
     */
    public void dismissProgressDialog() {

        try {
            if (loadDialog != null && loadDialog.isShowing()) {
                loadDialog.dismiss();
                loadDialog = null;
                text_show = null;
                progressView = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取进度弹框实例
     */
    public void getProgress(Context context) {
        if (progressView == null || text_show == null) {
            progressView = LayoutInflater.from(context).inflate(R.layout.dialog_load, null);
            text_show = (TextView) progressView.findViewById(R.id.text_show);
        }
        if (loadDialog == null) {
            loadDialog = new AlertDialog.Builder(context, R.style.CustomDialogStyle).create();
            loadDialog.setCanceledOnTouchOutside(false);
            loadDialog.requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
            loadDialog.show();
            loadDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
            loadDialog.setContentView(progressView);
        }
    }

    /******************************************* 等待的弹窗 end ****************************************/

    /**************
     * 展示自定义diaolog start
     ***********************/
    private Dialog customDialog;//自定义dialog


    private Context tempContext;//临时的context，用来判断是否是当前的dialog

    /**
     * 默认的dialog 视图样式，
     * 需要设置 {@link #setOnDialogBtnClick(OnDialogBtnClickListener)} 来实现具体的点击事件
     *
     * @param context
     * @param style 是一个HashMap，里边存储着，关键字分别为 title，desc，btnNega，btnPosi，为空则GONE
     */
    public void showCustomDialog(Context context, HashMap<String,String> style) {

        try {
            LogUtils.e("gzq", "tempContext = "+this.tempContext + "\nnewdContext = "+context);
            if (customDialog == null || this.tempContext != context) {
//                this.tempContext = context;
                customDialog = new AlertDialog.Builder(context, R.style.CustomDialogStyle).create();
                customDialog.setCanceledOnTouchOutside(false);
                customDialog.requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);

            }
            LogUtils.e("gzq", "show = "+customDialog.isShowing());
            if (!customDialog.isShowing()) {
                customDialog.show();
                customDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
                //如果为contentView == null，则使用默认的弹窗
                if(defaultView == null || this.tempContext != context){
                    LogUtils.e("gzq", "new defaultView");
                    defaultView = null;
                    defaultView = LayoutInflater.from(context).inflate(R.layout.view_normal_dialog, null);
                    tvTitle = (TextView) defaultView.findViewById(R.id.tv_title);
                    tvDesc = (TextView) defaultView.findViewById(R.id.tv_desc);
                    btnPosi = (TextView) defaultView.findViewById(R.id.btn_positive);
                    btnNega = (TextView) defaultView.findViewById(R.id.btn_negative);
                    btnDriver = defaultView.findViewById(R.id.btn_driver);
                }
                if(style != null && !style.isEmpty()){
                    if(TextUtils.isEmpty(style.get("title"))){
                        tvTitle.setVisibility(View.GONE);
                    }else{
                        tvTitle.setVisibility(View.VISIBLE);
                        tvTitle.setText(style.get("title"));
                    }

                    if(TextUtils.isEmpty(style.get("desc"))){
                        tvDesc.setVisibility(View.GONE);
                    }else{
                        tvDesc.setVisibility(View.VISIBLE);
                        tvDesc.setText(style.get("desc"));
                    }

                    if(TextUtils.isEmpty(style.get("btnPosi"))){
                        btnPosi.setVisibility(View.GONE);
                        btnDriver.setVisibility(View.GONE);
                    }else{
                        btnPosi.setVisibility(View.VISIBLE);
                        btnPosi.setText(style.get("btnPosi"));
                        btnPosi.setOnClickListener(this);
                    }

                    if(TextUtils.isEmpty(style.get("btnNega"))){
                        btnNega.setVisibility(View.GONE);
                        btnDriver.setVisibility(View.GONE);
                    }else{
                        btnNega.setVisibility(View.VISIBLE);
                        btnNega.setText(style.get("btnNega"));
                        btnNega.setOnClickListener(this);
                    }

                }

                customDialog.setContentView(defaultView);
                WindowManager.LayoutParams attributes = customDialog.getWindow().getAttributes();
                attributes.width = ViewSizeUtils.dp2px(context, 280);
                customDialog.getWindow().setAttributes(attributes);

                this.tempContext = context;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public interface OnDialogBtnClickListener {
        void onPositiveClick();
        void onNegativeClick();
    }


    public void setOnDialogBtnClick(OnDialogBtnClickListener mOnDialogBtnClickListener){
        this.mOnDialogBtnClickListener = mOnDialogBtnClickListener;
    }





    /**
     * 关闭自定义dialog
     */
    public void dismissCustomDialog() {
        try {
            if (customDialog != null && customDialog.isShowing()) {

                customDialog.dismiss();
                customDialog.setContentView(null);
                customDialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**************
     * 展示自定义diaolog end
     ***********************/


    private Context cTempContext;
    private AlertDialog dialog;

    /**
     * 布局自定义dialog
     *
     * @param contentView
     * @param context
     * @param params      Map类型，可定义动画，位置，是否触摸消失以及宽度
     */
    public void showCDialog(View contentView, Context context, HashMap<String, Object> params) {
        int anim = 0;
        int local = Gravity.CENTER;
        boolean touchOutside = true;
        int width = 0;

        if (params != null && !params.isEmpty()) {
            try {
                if (params.get("anim") != null) anim = (int) params.get("anim");
                if (params.get("local") != null) local = (int) params.get("local");
                if (params.get("touchOutside") != null)
                    touchOutside = (boolean) params.get("touchOutside");
                if (params.get("width") != null)
                    width = ViewSizeUtils.dp2px(context, (int) params.get("width"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {

            if (dialog == null || this.cTempContext != context) {
                this.cTempContext = context;
                dialog = new AlertDialog.Builder(context, R.style.CustomDialogStyle).create();
                dialog.setCanceledOnTouchOutside(touchOutside);
                dialog.requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);

            }
            if (!dialog.isShowing()) {
                dialog.show();

                dialog.getWindow().getAttributes().gravity = local;
                dialog.getWindow().setWindowAnimations(anim);

                if (width != 0) {
                    WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes();
                    attributes.width = width;
                    dialog.getWindow().setAttributes(attributes);
                }

                dialog.setContentView(contentView);


            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 关闭自定义dialog
     */
    public void dismissCDialog() {
        try {
            if (dialog != null && dialog.isShowing()) {

                dialog.dismiss();
                dialog.setContentView(null);
                dialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        if(v == null) return;
        switch(v.getId()){
            case R.id.btn_positive:
                DialogUtils.getInstance().dismissCustomDialog();
                if(mOnDialogBtnClickListener != null){
                    mOnDialogBtnClickListener.onPositiveClick();
                }
                break;
            case R.id.btn_negative:
                DialogUtils.getInstance().dismissCustomDialog();

                if(mOnDialogBtnClickListener != null){
                    mOnDialogBtnClickListener.onNegativeClick();
                }
                break;
        }
    }
}
